<template>
  <div class="standard-mode-panel">
    <!-- 标定设备选择面板 -->
    <div v-if="!selectedDevice" class="calibration-selection">
      <div class="header">
        <h1 class="title">请选择您需要标定的设备</h1>
        <div class="legend">
          <span class="note">[注]请将标定样架正确安装至对中台</span>
          <div class="status-legend">
            <span class="legend-item">
              <span class="status-indicator green"></span>
              绿色角标:未标定
            </span>
            <span class="legend-item">
              <span class="status-indicator grey"></span>
              灰色角标:已标定
            </span>
          </div>
        </div>
      </div>

      <div class="calibration-grid">
        <div
          v-for="item in calibrationItems"
          :key="item.id"
          class="calibration-card"
          :class="{ 'calibrated': item.isCalibrated }"
          @click="handleCardClick(item)"
        >
          <div class="card-header">
            <span class="card-title">{{ item.name }}</span>
            <span
              class="status-corner"
              :class="item.isCalibrated ? 'calibrated' : 'not-calibrated'"
            ></span>
          </div>
          <div class="card-body">
            <div v-if="item.calibrationTime" class="calibration-time">
              标定时间: {{ item.calibrationTime }}
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 标定内容面板 -->
    <div v-else class="tab-content">
      <router-view />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'

interface CalibrationItem {
  id: string
  name: string
  isCalibrated: boolean
  calibrationTime?: string
}

const router = useRouter()
const route = useRoute()
const selectedDevice = ref<string | null>(null)

const calibrationItems = ref<CalibrationItem[]>([
  { id: '1', name: '对中控制标定', isCalibrated: false },
  { id: '2', name: 'ACC标定', isCalibrated: true },
  { id: '3', name: 'ACCBack标定', isCalibrated: false },
  { id: '4', name: 'ACCAngle标定', isCalibrated: false },
  { id: '5', name: 'LDW1标定', isCalibrated: false },
  { id: '6', name: 'LDWBack1标定', isCalibrated: false },
  { id: '7', name: 'LDW2标定', isCalibrated: true, calibrationTime: '2024-01-15 14:30' },
  { id: '8', name: '吸波墙(前)标定', isCalibrated: false },
  { id: '9', name: '吸波墙(后)标定', isCalibrated: false },
  { id: '10', name: '360标定', isCalibrated: false },
  { id: '11', name: '轮眉控制标定', isCalibrated: false }
])

const handleCardClick = (item: CalibrationItem): void => {
  console.log('选择标定设备:', item.name)
  selectedDevice.value = item.name
  console.log('selectedDevice值:', selectedDevice.value)
  
  // 根据不同的标定设备名称路由到不同的路径
  const routeMap: Record<string, string> = {
    '对中控制标定': 'alignment-control',
    'ACC标定': 'acc-calibration',
    'ACCBack标定': 'accback-calibration',
    'ACCAngle标定': 'accangle-calibration',
    'LDW1标定': 'ldw1-calibration',
    'LDWBack1标定': 'ldwback1-calibration',
    'LDW2标定': 'ldw2-calibration',
    '吸波墙(前)标定': 'absorber-front-calibration',
    '吸波墙(后)标定': 'absorber-back-calibration',
    '360标定': '360-calibration',
    '轮眉控制标定': 'fender-control-calibration'
  }
  
  const routePath = routeMap[item.name] || 'alignment-control'
  router.push(`/calibration-control/${routePath}`)
}

// 监听路由变化，自动更新selectedDevice状态
watch(
  () => route.path,
  (newPath): void => {
    if (newPath === `/standard`) {
      selectedDevice.value = null
    } else if (newPath.startsWith('/calibration-control/')) {
      // 从路由参数中提取设备名称
      const type = route.params.type as string
      const nameMap: Record<string, string> = {
        'alignment-control': '对中控制标定',
        'acc-calibration': 'ACC标定',
        'accback-calibration': 'ACCBack标定',
        'accangle-calibration': 'ACCAngle标定',
        'ldw1-calibration': 'LDW1标定',
        'ldwback1-calibration': 'LDWBack1标定',
        'ldw2-calibration': 'LDW2标定',
        'absorber-front-calibration': '吸波墙(前)标定',
        'absorber-back-calibration': '吸波墙(后)标定',
        '360-calibration': '360标定',
        'fender-control-calibration': '轮眉控制标定'
      }
      selectedDevice.value = nameMap[type] || '对中控制标定'
    }
  },
  { immediate: true }
)
</script>

<style scoped>
.standard-mode-panel {
  padding: 24px;
  background-color: #f5f5f5;
  min-height: 100vh;
}

.calibration-selection {
  width: 100%;
}

.tab-content {
  width: 100%;
  height: 100%;
  padding: 0 20px 20px 20px;
}

/* 头部区域样式 */
.tab-content .header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 32px;
  background: white;
  padding: 24px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  min-height: 120px;
}

.tab-content .title-section {
  flex: 1;
}

.tab-content .main-title {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin: 0 0 16px 0;
}

.tab-content .sub-title {
  font-size: 20px;
  font-weight: 500;
  color: #666;
  margin: 0 0 16px 0;
  text-align: center;
}

.tab-content .instruction-note {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.tab-content .note-text {
  color: #e74c3c;
  font-size: 14px;
  line-height: 1.5;
}

.tab-content .global-actions {
  display: flex;
  gap: 12px;
}

/* 标定选择面板的头部样式 */
.calibration-selection .header {
  text-align: center;
  margin-bottom: 32px;
}

.title {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin: 0 0 16px 0;
}

.legend {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.note {
  color: #e74c3c;
  font-size: 14px;
}

.status-legend {
  display: flex;
  gap: 24px;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: #333;
}

.status-indicator {
  width: 12px;
  height: 12px;
  border-radius: 2px;
}

.status-indicator.green {
  background-color: #52c41a;
}

.status-indicator.grey {
  background-color: #8c8c8c;
}

.status-panel {
  width: 100%;
  height: 100%;
  flex: 1;
  overflow: auto;
  padding: 0 16px;
}
.calibration-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 16px;
  max-width: 1200px;
  margin: 0 auto;
}

.calibration-card {
  background: white;
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  position: relative;
}

.calibration-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
}

.calibration-card.calibrated {
  border: 2px solid #52c41a;
}

.card-header {
  background: white;
  padding: 16px;
  position: relative;
  min-height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.card-title {
  font-size: 14px;
  font-weight: 500;
  color: #333;
  text-align: center;
  line-height: 1.4;
}

.status-corner {
  position: absolute;
  top: 0;
  right: 0;
  width: 0;
  height: 0;
  border-style: solid;
}

.status-corner.not-calibrated {
  border-width: 0 20px 20px 0;
  border-color: transparent #52c41a transparent transparent;
}

.status-corner.calibrated {
  border-width: 0 20px 20px 0;
  border-color: transparent #8c8c8c transparent transparent;
}

.card-body {
  background: #e6f7ff;
  padding: 12px 16px;
  min-height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.calibration-time {
  font-size: 12px;
  color: #1890ff;
  text-align: center;
}

.content-layout {
  display: flex;
  flex-direction: column;
  height: calc(100vh - 120px);
}

.body {
  background: white;
  padding: 16px;
  position: relative;
  min-height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  flex: 1;
  overflow: auto;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .calibration-grid {
    grid-template-columns: repeat(4, 1fr);
  }
}

@media (max-width: 768px) {
  .calibration-grid {
    grid-template-columns: repeat(3, 1fr);
  }

  .status-legend {
    flex-direction: column;
    gap: 12px;
  }
}

@media (max-width: 480px) {
  .calibration-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .title {
    font-size: 20px;
  }
}
</style>
